<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@page import="br.net.woodstock.rockframework.util.PropertiesReader"%>
<%@page import="br.net.woodstock.tickettoride.client.beans.DefaultTicketRequest"%>
<%@page import="br.net.woodstock.tickettoride.client.beans.MapTicketConverter"%>
<%@page import="br.net.woodstock.tickettoride.client.beans.PropertiesTicketConverter"%>
<%@page import="br.net.woodstock.tickettoride.client.jdk6.TicketSessionHolder"%>
<%@page import="br.net.woodstock.tickettoride.client.TicketHash"%>
<%@page import="br.net.woodstock.tickettoride.client.TicketRequest"%>
<%@page import="br.net.woodstock.tickettoride.client.TicketSession"%>
<%@page import="br.net.woodstock.tickettoride.client.web.AbstractTicketToRideFilter"%>
<%@page import="java.io.ByteArrayInputStream"%>
<%@page import="java.util.Properties"%>
<%
	TicketHash hash = null;
	String app = "";
	String url = "";
	String expires = "";
	String data = "";
	if("true".equals(request.getParameter("doSubmit"))) {
		app = request.getParameter("application");
		url = request.getParameter("url");
		expires = request.getParameter("expires");
		data = request.getParameter("data");
		
		String [] array = app.split(",");
		
		PropertiesReader propertiesReader = PropertiesReader.getPlainTextInstance();
		Properties properties = propertiesReader.getProperties(new ByteArrayInputStream(data.getBytes()));
		
		TicketSession ticketSession = TicketSessionHolder.getInstance().getSession();
		PropertiesTicketConverter converter = new PropertiesTicketConverter();
		byte[] bytes = converter.toBytes(properties);
		TicketRequest ticketRequest = new DefaultTicketRequest(array, Integer.parseInt(expires), bytes);
		
		hash = ticketSession.send(ticketRequest);
	}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Ticket To Ride - Sample App</title>
</head>
<body>
	<form id="form" method="post" action="index.jsp">
		<input type="hidden" name="doSubmit" value="true" />
		<table>
			<tr>
				<td>Application:<td>
				<td><input type="text" name="application" value="<%=app%>" /></td>
			</tr>
			<tr>
				<td>URL:<td>
				<td><input type="text" name="url" size="100" value="<%=url%>" /></td>
			</tr>
			<tr>
				<td>Expires:<td>
				<td><input type="text" name="expires" size="5"  value="<%=expires%>" /></td>
			</tr>
			<tr>
				<td>Data:<td>
				<td><textarea rows="10" cols="80" name="data"><%=data%></textarea></td>
			</tr>
		</table>
		<br />
		<%
			if(hash != null) {
		%>
			Hash:
			<a href="<%=url%>hash=<%=hash.getHash()%>" target="_blank">
				<%=hash%>
			</a>
			<br />
			Filter:
			<a href="<%=url%><%=AbstractTicketToRideFilter.TICKET_HASH_PARAMETER%>=<%= hash.getHash() %>" target="_blank">
				<%= hash %>
			</a>
			<br />
		<% } %>
		<input type="submit" name="submit" value="Enviar" />
	</form>
</body>
</html>